package com.example.firstjavamqtt.service.base.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.firstjavamqtt.common.excepiton.GlobalException;
import com.example.firstjavamqtt.common.result.ResultCodeEnum;
import com.example.firstjavamqtt.mapper.DeviceMapper;
import com.example.firstjavamqtt.model.base.Device;
import com.example.firstjavamqtt.service.base.DeviceService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> implements DeviceService {

    //注册设备
    @Override
    public void registerDevice(Device device) {
        Device exist = baseMapper.selectOne(getQueryWrapperByDeviceId(device.getDeviceId()));
        //如果存在并且  抛出重复注册异常
        if (exist != null) {
            throw new GlobalException(ResultCodeEnum.REPEAT_REGISTER);
        }
        baseMapper.insert(device);
    }

    //本来应该是要根据用户id查询所有设备的 但是我没弄用户id 就直接查询所有设备吧
    @Override
    public List<Device> getDeviceList() {
        return baseMapper.selectList(null);
    }

    //根据设备id删除设备
    @Override
    public void deleteDeviceByDeviceId(String deviceId) {
        int count = baseMapper.delete(getQueryWrapperByDeviceId(deviceId));
        if (count < 1) {
            throw new GlobalException(ResultCodeEnum.PARAM_ERROR);
        }
    }

    //设备上下线状态
    @Override
    public void setDeviceStatus(String deviceId, boolean online) {
        //首先查询有无该设备
        Device device = this.getDeviceByDeviceId(deviceId);
        if (device == null) {
            throw new GlobalException(ResultCodeEnum.ERROR_DEVICE_ID);
        }
        //如果状态
        device.setOnline(online);
        //如果是在线状态 更新最后在线时间
        if (online) {
            device.setLastOnlineDate(new Date());
        }
        //更新
        baseMapper.update(device, getQueryWrapperByDeviceId(deviceId));
    }

    //全部下线
    @Override
    public void setAllDeviceOffline() {
        List<Device> deviceList = this.getDeviceList();
        deviceList.forEach(item -> item.setOnline(false));
        updateBatchById(deviceList);
    }

    //根据设备id获取设备
    @Override
    public Device getDeviceByDeviceId(String deviceId) {
        Device device = baseMapper.selectOne(getQueryWrapperByDeviceId(deviceId));
        if (device == null) {
            throw new GlobalException(ResultCodeEnum.ERROR_DEVICE_ID);
        }
        return device;
    }

    //根据设备类型获取设备
    @Override
    public List<Device> getDeviceByDeviceType(String deviceType) {
        return baseMapper.selectList(getWrapperByDeviceType(deviceType, "device_type"));
    }

    @Override
    public void deleteDeviceByDeviceType(String deviceType) {
        int i = baseMapper.delete(getWrapperByDeviceType(deviceType, "device_type"));
        if (i < 1) {
            throw new GlobalException(ResultCodeEnum.PARAM_ERROR);
        }
    }

    @Override
    public void deleteDeviceByBatchId(ArrayList<Integer> batchId) {
        int i = baseMapper.deleteBatchIds(batchId);
        if (i < 1) {
            throw new GlobalException(ResultCodeEnum.PARAM_ERROR);
        }
    }

    @Override
    public void deleteDeviceById(int id) {
        int i = baseMapper.deleteById(id);
        if (i < 1) {
            throw new GlobalException(ResultCodeEnum.PARAM_ERROR);
        }
    }

    @Override
    public Page<Device> getDevicePageList(int pageNum, int limit) {
        Page<Device> page = new Page<>(pageNum, limit);
        return baseMapper.selectPage(page, null);

    }

    //根据deviceType获取wrapper
    private QueryWrapper<Device> getWrapperByDeviceType(String deviceType, String device_type) {
        return new QueryWrapper<Device>().eq(device_type, deviceType);
    }

    //根据deviceId获取wrapper
    private QueryWrapper<Device> getQueryWrapperByDeviceId(String deviceId) {
        return getWrapperByDeviceType(deviceId, "device_id");
    }
}
